package com.ksm.client.document.presenter;

import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.inject.Inject;
import com.ksm.client.ManagerEventBus;
import com.ksm.client.document.presenter.DocumentFormPresenter.IDocumentFormView;
import com.ksm.client.document.service.IKsmServiceAsync;
import com.ksm.client.document.view.DocumentFormView;
import com.ksm.transfer.dto.DocumentDTO;
import com.ksm.transfer.dto.TvaDTO;
import com.ksm.transfer.dto.TypeDocumentDTO;
import com.mvp4g.client.annotation.Presenter;
import com.mvp4g.client.presenter.BasePresenter;
import com.mvp4g.client.view.ReverseViewInterface;
import com.sencha.gxt.widget.core.client.form.ComboBox;
import com.sencha.gxt.widget.core.client.form.DateField;
import com.sencha.gxt.widget.core.client.form.NumberField;
import com.sencha.gxt.widget.core.client.form.TextArea;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.info.Info;

@Presenter(view = DocumentFormView.class)
public class DocumentFormPresenter extends BasePresenter<IDocumentFormView, ManagerEventBus> implements IDocumentFormPresenter {

	@Inject
	IKsmServiceAsync ksmService;

	public interface IDocumentFormView extends IsWidget, ReverseViewInterface<IDocumentFormPresenter> {

		public DateField getDate();

		public void setDate(DateField date);

		public NumberField<Double> getMontant();

		public void setMontant(NumberField<Double> montant);

		public TextField getInterlocuteur();

		public void setInterlocuteur(TextField interlocuteur);

		public TextArea getCommentaire();

		public void setCommentaire(TextArea commentaire);

		public Long getIdDocument();

		public void setIdDocument(Long idDocument);

		public ComboBox<TypeDocumentDTO> getComboType();

		public void setComboType(ComboBox<TypeDocumentDTO> comboType);

		public ComboBox<TvaDTO> getComboTVA();

		public void setComboTVA(ComboBox<TvaDTO> comboTVA);

		public void initComboTva(List<TvaDTO> elements);

		public void initComboType(List<TypeDocumentDTO> elements);

		public void resetForm();

		public void setTva(Long tvaId);

		public void setType(Long typeId);
	}

	@Override
	public void saveDocument() {
		DocumentDTO documentDTO = new DocumentDTO();
		documentDTO.setDate(getView().getDate().getValue());
		documentDTO.setMontant(getView().getMontant().getValue());
		documentDTO.setType(getView().getComboType().getValue().getName());
		documentDTO.setCommentaire(getView().getCommentaire().getValue());
		documentDTO.setInterlocuteur(getView().getInterlocuteur().getValue());
		documentDTO.setId(getView().getIdDocument());
		documentDTO.setTauxTvaId(getView().getComboTVA().getValue().getId());
		documentDTO.setTypeDocumentId(getView().getComboType().getValue().getId());

		ksmService.saveDocument(documentDTO, new AsyncCallback<DocumentDTO>() {

			@Override
			public void onSuccess(DocumentDTO documentDTO) {
				Info.display("Save OK", "Save OK");
				getEventBus().documentSaved(documentDTO);
			}

			@Override
			public void onFailure(Throwable throwable) {
				Info.display("Save error", "Save error" + throwable.getMessage());
			}
		});
	}

	@Override
	public void onDocumentGridLoad(DocumentDTO documentDTO) {
		getView().setIdDocument(documentDTO.getId());
		getView().getMontant().setValue(documentDTO.getMontant());
		getView().getDate().setValue(documentDTO.getDate());
		getView().getCommentaire().setValue(documentDTO.getCommentaire());
		getView().getInterlocuteur().setValue(documentDTO.getInterlocuteur());
		getView().setTva(documentDTO.getTauxTvaId());
		getView().setType(documentDTO.getTypeDocumentId());
	}

	public void onDocumentSaved() {

	}

	@Override
	public void onDocumentNewLoad() {
		getView().resetForm();
	}

	public void onReferentialTvaUpdate(List<TvaDTO> tvaDTOs) {
		view.initComboTva(tvaDTOs);
	}

	public void onReferentialTypeDocumentUpdate(List<TypeDocumentDTO> typeDocumentDTOs) {
		view.initComboType(typeDocumentDTOs);
	}
}
